package com.jeesite.modules.own.utils;

import com.jeesite.modules.own.dao.ChainTaskDao;
import com.jeesite.modules.own.entity.*;
import com.jeesite.modules.own.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

import static com.jeesite.modules.Application.httpflag;
import static org.springframework.util.StringUtils.isEmpty;

@EnableScheduling
@Configuration
public class ChainTask {

    @Autowired
    private ChainTaskDao chainTaskDao;
    @Autowired
    private OwnMedicineAssignmentService ownMedicineAssignmentService;
    @Autowired
    private OwnMedicineFieldManagementService ownMedicineFieldManagementService;
    @Autowired
    private OwnMedicineGrowerInformationService ownMedicineGrowerInformationService;
    @Autowired
    private OwnMedicineInspectionInformationService ownMedicineInspectionInformationService;
    @Autowired
    private OwnMedicinePlantInformationService ownMedicinePlantInformationService;
    @Autowired
    private OwnMedicineReceiptInformationService ownMedicineReceiptInformationService;
    @Autowired
    private OwnMedicineSubcontractingService ownMedicineSubcontractingService;
    @Autowired
    private OwnOutContentsService ownOutContentsService;
    @Autowired
    private OwnOutInformationService ownOutInformationService;
    @Autowired
    private MessageSaveUtils messageSaveUtils;

    @Value("${qkurl.saveUrl}")
    private  String saveUrl;

    @Scheduled(cron = "0/30 * * * * ?")
    private void configureTasks() {
        String s = HttpClient1.doPost(saveUrl, "");
        if(isEmpty(s)){
            httpflag=false;
        }else{
            httpflag=true;
        }
        //每隔50秒查询数据库，查看每条数据的txid是否包含errortxid，如果包含则表示该条数据因请求超时未能上链成功，重新上链并把新的txid保存到数据库
        List<OwnMedicineAssignment> ownMedicineAssignment = chainTaskDao.getOwnMedicineAssignment();
        for(OwnMedicineAssignment medicineAssignment:ownMedicineAssignment){
            if(medicineAssignment.getTxid().contains("errortxid")){
                String[] errortxids = medicineAssignment.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicineAssignment.setTxid(txids);
                ownMedicineAssignmentService.save(medicineAssignment);
            }
        }

        List<OwnMedicineFieldManagement> ownMedicineFieldManagement = chainTaskDao.getOwnMedicineFieldManagement();
        for(OwnMedicineFieldManagement medicineFieldManagement:ownMedicineFieldManagement){
            if(medicineFieldManagement.getTxid().contains("errortxid")){
                String[] errortxids = medicineFieldManagement.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicineFieldManagement.setTxid(txids);
                ownMedicineFieldManagementService.save(medicineFieldManagement);
            }
        }

        List<OwnMedicineGrowerInformation> ownMedicineGrowerInformation = chainTaskDao.getOwnMedicineGrowerInformation();
        for (OwnMedicineGrowerInformation medicineGrowerInformation:ownMedicineGrowerInformation) {
            if(medicineGrowerInformation.getTxid().contains("errortxid")){
                String[] errortxids = medicineGrowerInformation.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicineGrowerInformation.setTxid(txids);
                ownMedicineGrowerInformationService.save(medicineGrowerInformation);

            }
        }

        List<OwnMedicineInspectionInformation> ownMedicineInspectionInformation = chainTaskDao.getOwnMedicineInspectionInformation();
        for(OwnMedicineInspectionInformation medicineInspectionInformation:ownMedicineInspectionInformation){
            if(medicineInspectionInformation.getTxid().contains("errortxid")){
                String[] errortxids = medicineInspectionInformation.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicineInspectionInformation.setTxid(txids);
                ownMedicineInspectionInformationService.save(medicineInspectionInformation);
            }

        }

        List<OwnMedicinePlantInformation> ownMedicinePlantInformation = chainTaskDao.getOwnMedicinePlantInformation();
        for(OwnMedicinePlantInformation medicinePlantInformation:ownMedicinePlantInformation){
            if(medicinePlantInformation.getTxid().contains("errortxid")){
                String[] errortxids = medicinePlantInformation.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicinePlantInformation.setTxid(txids);
                ownMedicinePlantInformationService.save(medicinePlantInformation);

            }
        }

        List<OwnMedicineReceiptInformation> ownMedicineReceiptInformation = chainTaskDao.getOwnMedicineReceiptInformation();
        for(OwnMedicineReceiptInformation medicineReceiptInformation:ownMedicineReceiptInformation){
            if(medicineReceiptInformation.getTxid().contains("errortxid")){
                String[] errortxids = medicineReceiptInformation.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicineReceiptInformation.setTxid(txids);
                ownMedicineReceiptInformationService.save(medicineReceiptInformation);
            }

        }

        List<OwnMedicineSubcontracting> ownMedicineSubcontracting = chainTaskDao.getOwnMedicineSubcontracting();
        for(OwnMedicineSubcontracting medicineSubcontracting:ownMedicineSubcontracting){
            if(medicineSubcontracting.getTxid().contains("errortxid")){
                String[] errortxids = medicineSubcontracting.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                medicineSubcontracting.setTxid(txids);
                ownMedicineSubcontractingService.save(medicineSubcontracting);

            }
        }

        List<OwnOutContents> ownOutContents = chainTaskDao.getOwnOutContents();
        for(OwnOutContents outContents:ownOutContents){
            if(outContents.getTxid().contains("errortxid")){
                String[] errortxids = outContents.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                outContents.setTxid(txids);
                ownOutContentsService.save(outContents);
            }

        }

        List<OwnOutInformation> ownOutInformation = chainTaskDao.getOwnOutInformation();
        for(OwnOutInformation outInformation:ownOutInformation){
            if(outInformation.getTxid().contains("errortxid")){
                String[] errortxids = outInformation.getTxid().split("errortxid");
                String errortxid = errortxids[1];
                String txids = messageSaveUtils.getTxids(errortxid);
                outInformation.setTxid(txids);
                ownOutInformationService.save(outInformation);
            }

        }
    }

}
